package com.cy.pj.goods.controller;

import com.cy.pj.goods.dao.GoodsDao;
import com.cy.pj.goods.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsDao goodsDao;

    @RequestMapping("/doFindAll")
    public String doFindAll(Model model){
        List<Goods> list = goodsDao.findAll();
        model.addAttribute("list", list);
        return "goods";
    }

    @RequestMapping("/doDeleteById")
    public String doDeleteById(Integer id, Model model){
        goodsDao.delete(id);
        //List<Goods> list = goodsDao.findAll();
        //model.addAttribute("list", list);
        //return "goods";
        return "forward:doFindAll";
    }

    @RequestMapping("/doInsert")
    public String doInsert(){
        return "goods_add";
    }

    @RequestMapping("/doSaveGoods")
    public String doSaveGoods(Goods goods ,Model model){
        goodsDao.insert(goods);
        /*List<Goods> list = goodsDao.findAll();
        model.addAttribute("list", list);
        return "goods";*/
        return "forward:doFindAll";
    }

    @RequestMapping("/doFindById")//带数据到html页面
    public String findById(Integer id, Model model){
        Goods goods = goodsDao.findById(id);
        model.addAttribute("goods", goods);
        return "goods_update";
    }

    @RequestMapping("/doUpdateGoods")
    public String doUpdateGoods(Goods goods, Model model){
        goodsDao.update(goods);
        /*List<Goods> list = goodsDao.findAll();
        model.addAttribute("list", list);
        return "goods";*/
        return "forward:doFindAll";
    }










}
